Secondary index and indexed view maintenance for updates to complex types

ABSTRACT

Pursuant to receipt of a data modification command or statement, such as an Update command, it is determined which fields in a data structure, such as a UDT, computed column, indexed view, or index, for example, will be changed as a result of the command. Only those fields that are affected by the command will be changed. Thus, changes are propagated to only to those dependent data structures whose content is being modified. Tree representations are used to facilitate the determination as to whether a subfield in a hierarchy is changed.

CROSS-REFERENCE TO RELATED CASES

The instant application is a divisional application of U.S. applicationSer. No. 10/891,220 filed Jul. 14, 2004, entitled “Improving SecondaryIndex and Indexed View Maintenance for Updates to Complex Types” whichis incorporated by reference herein in its entirety.

FIELD OF THE INVENTION

The present invention relates generally to the field of informationstorage and retrieval, and, more particularly, to query optimization andmanagement.

BACKGROUND OF THE INVENTION

Traditional approaches to the organization of information in computersystems have centered on the use of file-folder-and-directory-basedsystems (“file systems”) to organize files into directory hierarchies offolders based on an abstraction of the physical organization of thestorage medium used to store the files. Typically, storable data hasbeen organized into files, folders, and directories at the operatingsystem level. These files generally include the file hierarchy itself(the “directory”) embodied in a special file maintained by the filesystem. This directory, in turn, maintains a list of entriescorresponding to all of the other files in the directory and the nodallocation of such files in the hierarchy (herein referred to as thefolders). Such has been the state of the art for approximately fortyyears.

However, while providing a reasonable representation of informationresiding in the computer's physical storage system, a file system isnevertheless an abstraction of that physical storage system, andtherefore utilization of the files requires a level of interpretationbetween what the user manipulates (units having context, features, andrelationships to other units) and what the operating system provides(files, folders, and directories). Consequently, users (applicationsand/or end-users) have no choice but to force units of information intoa file system structure even when doing so is inefficient, inconsistent,or otherwise undesirable. Because most existing file systems utilize anested folder metaphor for organizing files and folders, as the numberof files increases, the effort necessary to maintain an organizationscheme that is flexible and efficient becomes quite daunting.

Several unsuccessful attempts to address the shortcomings of filesystems have been made in the past. Some of these previous attempts haveinvolved the use of content addressable memory to provide a mechanismwhereby data could be accessed by content rather than by physicaladdress. However, these efforts have proven unsuccessful because, whilecontent addressable memory has proven useful for small-scale use bydevices such as caches and memory management units, large-scale use fordevices such as physical storage media has not yet been possible for avariety of reasons, and thus such a solution simply does not exist.Other attempts using object-oriented database (OODB) systems have beenmade, but these attempts, while featuring strong databasecharacteristics and good non-file representations, were not effective inhandling file representations and could not replicate the speed,efficiency, and simplicity of the file-and-folder-based hierarchicalstructure at the hardware/software interface system level.

Newly developed storage systems, such as “WinFS” (described furtherbelow) store the directory of the files as table(s) in a database. Eachfile is represented by a row in a base table, and file systemoperations, such as “enumerate all files in a directory”, are satisfiedusing queries against the database engine. Thus, efficiently performingbasic operations against the store involves efficiently optimizingdatabase queries.

In such storage systems, the concept of a file is extended to that of an“object”. Metadata about the file is stored in a managed CLR (commonlanguage runtime) object with a schema (defined in the storage system)to represent the allowable descriptive data for that object. Forexample, a picture would have a representative CLR object that wouldstore data such as its resolution, time it was taken, and locationinformation. This object model supports data inheritance. With datainheritance, it is possible to derive a type from another and add newfields. For example, a sub-class of the picture could be created, suchas “DriversLicensePicture”. Such a sub-class would contain extrainformation, such as a Driver's License ID field.

In these newly developed storage systems, such as WinFS, the exposedschemas are mapped to tables through a translation layer. Users only seea series of views of the data instead of operating on the base tables.While the exact design of this mapping is not significant, it serves asthe glue between the WinFS API and the underlying storage format. Usersdo not control or see this mapping directly.

The WinFS store also exposes the concept of querying objects based ontheir type, as opposed to their file name as in earlier conventionalfile systems. Type-based queries can search for an exact type or anytype that derives from a given type. This latter form is calledhierarchical matching, and it is expected to be a common WinFSoperation. WinFS also supports searching by file.

WinFS's schema model poses some new challenges to the query processor.User-defined types, or UDTs, are used extensively, and it is common toretrieve all UDTs from a table based on the UDT type. Furthermore, WinFSuses UDT inheritance, and it is also a requirement to retrieve allelements of a given type and also any subtype from a table. Multipletables exist, each containing a different number of UDTs, types, typetopology, and UDT distribution within that topology. Additionally,searching operations may go beyond those operations seen in traditionalrelational database systems to include, for example, searching of XMLdocuments or performing searches over all fields in an object. Theseproperties make it difficult to make accurate cardinality and costestimates, and also make it difficult to efficiently retrieve valuesbased on type/subtype hierarchy.

Materialized views (also referred to herein as indexed views) have beena subject of database research for over a decade. The basic idea is tomaterialize, or store, the result of some query, then use such computedresult when similar queries are submitted to the database. For example,it may be desirable to store the result of sales per day, for example,and use the result (this materialized view) in the future to answerseveral related queries, such as sales in a given month or total salesin the year.

For additional flexibility, applications should not need to be awarethat certain views exist, or are materialized. The query processorshould identify matches between user queries and existing pre-computedresults (materialized views), and use such results when applicable. Thisis known as the view utilization problem: Given a user query writtenover base tables, as well as a collection of materialized views, whichmaterialized views can be used to answer such query? And the cost-basedvariant of the question: Which of those materialized views should beused?

Materialized views are similar to indices, in that they should be partof the physical design of the database and their primary purpose is toimprove performance. The logical design of the database, and correctnessof applications should be independent of the presence or absence ofmaterialized views. As with indices, materialized views can introducedramatic improvements in query performance.

Query optimizers are normally structured such that there is an initialsimplification stage, followed by exploration of alternatives andcost-based selection of an execution plan, as shown in FIG. 1.

During the simplification/normalization stage 2, some changes are madeon the original query Q, such as pushing selections down, or rewriting asubquery as a join, when possible. These modifications are aimed atobtaining a “better” query. Typically, there is no detailed costestimation at this stage, and a single “better” query Q′ is produced asthe result.

The second stage 5 (exploration and cost-based selection) inoptimization is directed to generating multiple alternatives, and usinga detailed cost model to select the alternative with the cheapestestimated execution cost. Two conventional architectures for theexploration stage are bottom-up, dynamic programming join enumeration,and transformation-driven generation of alternatives. Both architecturesset up a table of alternatives, as is well known, which compactlyencodes the various possibilities for each sub-expression of a query.

The SQL query language provides a user with the ability to query (andmodify) tabular data stored using the relational data model. Therelational data model dictates that each cell in a table (a column of arow) is a single scalar (or atomic) value. The structured query language(SQL) is an American National Standards Institute (ANSI) standard usedto communicate with a relational database. SQL statements are used toperform tasks such as update data or retrieve data from a relationaldatabase. Although many database systems use SQL, many of them also havetheir own additional proprietary extensions that are usually only usedon their system. However, the standard SQL commands such as “Select”,“Insert”, “Update”, “Delete”, “Create”, and “Drop” can be used toaccomplish many things that one desires to do with a relational databaseand are believed to be well known to those skilled in the database art.

The SQL Server database management system (DBMS) supports secondaryindexes and indexed views built over tables, to speed up certain kindsof data retrieval operations. A secondary index is a data structureorganized like a tree, which contains a subset of the columns belongingto the table. An indexed view is the pre-computed result of a querystored into a derived table, which can have secondary indexes as well.Secondary indexes and indexed views are referred to as data structuresdependent on the table. Other kinds of dependent objects exist, such asconstraints.

When an Insert, Update, or Delete statement is processed, the DBMS hasto propagate the change from the table to the dependent structures, sothat they are always kept consistent which each other. This allowssubsequent data retrieval queries to return the same data independentlyon whether they access the table, a secondary index, or an indexed view.Thus, whenever modifications are made to the table, they must be made tothe secondary indexes and indexed views, as well. The cost of makingchanges is proportional to the number of places where the change has tobe made. While Insert and Delete operations need to be propagated to allthe secondary indexes and indexed views, Update statement processing isconventionally optimized to propagate the change only to the datastructures that carry columns being modified by the statement. Forexample, if a secondary index only contains the column C1 of a table T,an update to the C2 column will not be propagated to the index, becauseits content will be unchanged.

Both secondary indexes and indexed views can be built in SQL Server overboth regular table columns and scalar expressions over table columns.For example, it is possible to build a secondary index over the sum ofthe C1 and C2 columns. Conventionally, whenever an Update statementaffects one or more of the columns participating in a scalar expression,the expression will be assumed to be changing, and the change will bepropagated to all the dependent data structures that carry thisexpression.

Support for complex data types has been added to SQL Server throughUDTs. The syntax of the SQL Update statement allows granular updates tosubfields of a UDT, along with the ability to overwrite an entire columnwith a new value. However, by using the traditional technique describedabove for scalar expressions, a granular update to a UDT would beassumed to affect all the data structures dependent on any of itssubfields, and thus changes are propagated to dependent data structureswhose content might not be modified by the update. This technique isinefficient and expensive. In other words, conventionally, granularupdates to UDT columns are assumed to affect all its subfields, therebydegrading performance in the presence of many dependent access paths.

For example, in the prior art, for an Update, it is assumed that if anyof the terms in the expression are being changed, then the entireexpression is being changed. For example, in the computed columnPERSON.ADDRESS.ZIP, when any of PERSON's subfields are updated, it isassumed that the address and zip also change, so the computed column isalways updated. This is a very conservative approach, and not desirable.

In view of the foregoing, there is a need for systems and methods thatovercome the limitations and drawbacks of the prior art.

SUMMARY OF THE INVENTION

The following summary provides an overview of various aspects of theinvention. It is not intended to provide an exhaustive description ofall of the important aspects of the invention, nor to define the scopeof the invention. Rather, this summary is intended to serve as anintroduction to the detailed description and figures that follow.

Pursuant to receipt of a data modification command or statement, such asan Update command, it is determined which fields in a data structure,such as a user-defined type (UDT), computed column, indexed view, orindex, for example, will be changed as a result of the command. Onlythose fields that are affected by the command will be changed. Thus,changes are propagated to only to those dependent data structures whosecontent is being modified. Tree representations are used to facilitatethe determination as to whether a subfield in a hierarchy is changed.

Exemplary embodiments include a method of processing a data modificationstatement, comprising receiving a data modification statement,determining which fields of a data structure are affected by the datamodification statement, and modifying a query plan in response to thefields of the data structure that are affected by the data modificationstatement.

According to aspects of the invention, a tree representation for thedata modification statement is determined, along with operator treesrepresenting the expressions of the secondary structures related to theinvolved tables. Both trees are compared to determine which secondarystructures are affected by the data modification statement.

Another exemplary embodiment is directed to a system for processing adata modification statement, comprising a front end that receives a datamodification statement, and a query optimizer that determines whichfields of a data structure are affected by the data modificationstatement, and modifies a query plan responsive to the fields of thedata structure that are affected by the data modification statement.

According to aspects of the invention, the front end comprises a parserand algebrizer that transforms the textual representation of the datamodification statement into tree nodes. The front end may be adapted todetermine a tree representation for the data modification statement andfor the expressions of secondary structures. The query optimizer may beadapted to determine which secondary structures are affected by the datamodification statement by comparing the tree representation for thesecondary structures and the tree representation for the datamodification statement.

Additional features and advantages of the invention will be madeapparent from the following detailed description of illustrativeembodiments that proceeds with reference to the accompanying drawings.

BRIEF DESCRIPTION OF THE DRAWINGS

The foregoing summary, as well as the following detailed description ofpreferred embodiments, is better understood when read in conjunctionwith the appended drawings. For the purpose of illustrating theinvention, there is shown in the drawings exemplary constructions of theinvention; however, the invention is not limited to the specific methodsand instrumentalities disclosed. In the drawings:

FIG. 1 is a block diagram of a conventional query optimizer;

FIG. 2 is a block diagram representing a computer system in whichaspects of the present invention may be incorporated;

FIG. 3 is a block diagram illustrating a computer system divided intothree component groups: the hardware component, the operating systemcomponent, and the applications programs component;

FIG. 4 illustrates an exemplary storage platform that can be used withthe present invention;

FIG. 5 shows a flow diagram of an exemplary method of processing a datamodification statement in accordance with the present invention;

FIG. 6 is a flow diagram of an example that is useful in describing thepresent invention;

FIG. 7 is a block diagram of an exemplary system in accordance with thepresent invention; and

FIGS. 8, 9, and 10 show exemplary tree representations that are usefulin describing the present invention.

DETAILED DESCRIPTION OF PREFERRED EMBODIMENTS

The subject matter is described with specificity to meet statutoryrequirements. However, the description itself is not intended to limitthe scope of this patent. Rather, the inventors have contemplated thatthe claimed subject matter might also be embodied in other ways, toinclude different steps or combinations of steps similar to the onesdescribed in this document, in conjunction with other present or futuretechnologies. Moreover, although the term “step” may be used herein toconnote different elements of methods employed, the term should not beinterpreted as implying any particular order among or between varioussteps herein disclosed unless and except when the order of individualsteps is explicitly described.

Exemplary Computing Environment

Numerous embodiments of the present invention may execute on a computer.FIG. 2 and the following discussion are intended to provide a briefgeneral description of a suitable computing environment in which theinvention may be implemented. Although not required, the invention willbe described in the general context of computer executable instructions,such as program modules, being executed by a computer, such as a clientworkstation or a server. Generally, program modules include routines,programs, objects, components, data structures and the like that performparticular tasks or implement particular abstract data types.

An “object” is a unit of storable information accessible to ahardware/software interface system that has a basic set of propertiesthat are commonly supported across all objects exposed to an end-user bythe hardware/software interface system shell. Objects also haveproperties and relationships that are commonly supported across alltypes including features that allow new properties and relationships tobe introduced. A complex structured type consists of a set of fields,properties, and methods. Each field or property can be one of a scalartype, a complex structure type itself, or of a multiset in which eachelement is a complex structured type.

Those skilled in the art will appreciate that the invention may bepracticed with other computer system configurations, including handhelddevices, multiprocessor systems, microprocessor based or programmableconsumer electronics, network PCs, minicomputers, mainframe computersand the like. The invention may also be practiced in distributedcomputing environments where tasks are performed by remote processingdevices that are linked through a communications network. In adistributed computing environment, program modules may be located inboth local and remote memory storage devices.

As shown in FIG. 2, an exemplary general purpose computing systemincludes a conventional personal computer 20 or the like, including aprocessing unit 21, a system memory 22, and a system bus 23 that couplesvarious system components including the system memory to the processingunit 21. The system bus 23 may be any of several types of bus structuresincluding a memory bus or memory controller, a peripheral bus, and alocal bus using any of a variety of bus architectures. The system memoryincludes read only memory (ROM) 24 and random access memory (RAM) 25. Abasic input/output system 26 (BIOS), containing the basic routines thathelp to transfer information between elements within the personalcomputer 20, such as during start up, is stored in ROM 24.

The personal computer 20 may further include a hard disk drive 27 forreading from and writing to a hard disk, not shown, a magnetic diskdrive 28 for reading from or writing to a removable magnetic disk 29,and an optical disk drive 30 for reading from or writing to a removableoptical disk 31 such as a CD-ROM or other optical media. The hard diskdrive 27, magnetic disk drive 28, and optical disk drive 30 areconnected to the system bus 23 by a hard disk drive interface 32, amagnetic disk drive interface 33, and an optical drive interface 34,respectively. The drives and their associated computer readable mediaprovide nonvolatile storage of computer readable instructions, datastructures, program modules and other data for the personal computer 20.

Although the exemplary environment described herein employs a hard disk,a removable magnetic disk 29 and a removable optical disk 31, it shouldbe appreciated by those skilled in the art that other types of computerreadable media which can store data that is accessible by a computer,such as magnetic cassettes, flash memory cards, digital video disks,Bernoulli cartridges, random access memories (RAMs), read only memories(ROMs) and the like may also be used in the exemplary operatingenvironment.

A number of program modules may be stored on the hard disk, magneticdisk 29, optical disk 31, ROM 24 or RAM 25, including an operatingsystem 35, one or more application programs 36, other program modules 37and program data 38. A user may enter commands and information into thepersonal computer 20 through input devices such as a keyboard 40 andpointing device 42. Other input devices (not shown) may include amicrophone, joystick, game pad, satellite disk, scanner or the like.These and other input devices are often connected to the processing unit21 through a serial port interface 46 that is coupled to the system bus,but may be connected by other interfaces, such as a parallel port, gameport or universal serial bus (USB). A monitor 47 or other type ofdisplay device is also connected to the system bus 23 via an interface,such as a video adapter 48. In addition to the monitor 47, personalcomputers typically include other peripheral output devices (not shown),such as speakers and printers. The exemplary system of FIG. 2 alsoincludes a host adapter 55, Small Computer System Interface (SCSI) bus56, and an external storage device 62 connected to the SCSI bus 56.

The personal computer 20 may operate in a networked environment usinglogical connections to one or more remote computers, such as a remotecomputer 49. The remote computer 49 may be another personal computer, aserver, a router, a network PC, a peer device or other common networknode, and typically includes many or all of the elements described aboverelative to the personal computer 20, although only a memory storagedevice 50 has been illustrated in FIG. 2. The logical connectionsdepicted in FIG. 2 include a local area network (LAN) 51 and a wide areanetwork (WAN) 52. Such networking environments are commonplace inoffices, enterprise wide computer networks, intranets and the Internet.

When used in a LAN networking environment, the personal computer 20 isconnected to the LAN 51 through a network interface or adapter 53. Whenused in a WAN networking environment, the personal computer 20 typicallyincludes a modem 54 or other means for establishing communications overthe wide area network 52, such as the Internet. The modem 54, which maybe internal or external, is connected to the system bus 23 via theserial port interface 46. In a networked environment, program modulesdepicted relative to the personal computer 20, or portions thereof, maybe stored in the remote memory storage device. It will be appreciatedthat the network connections shown are exemplary and other means ofestablishing a communications link between the computers may be used.

While it is envisioned that numerous embodiments of the presentinvention are particularly well-suited for computerized systems, nothingin this document is intended to limit the invention to such embodiments.On the contrary, as used herein the term “computer system” is intendedto encompass any and all devices comprising press buttons, or capable ofdetermining button presses, or the equivalents of button presses,regardless of whether such devices are electronic, mechanical, logical,or virtual in nature.

As illustrated in the block diagram of FIG. 3, a computer system 300 canbe roughly divided into three component groups: the hardware component302, the operating system component 304, and the applications programscomponent 306.

In certain computer systems 300, and referring back to FIG. 2, thehardware 302 may comprise the central processing unit (CPU) 21, thememory (both ROM 24 and RAM 25), the basic input/output system (BIOS)26, and various input/output (I/O) devices such as a keyboard 40, amouse 42, a monitor 47, and/or a printer (not shown), among otherthings. The hardware component 302 comprises the basic resources for thecomputer system 300.

The applications programs component 306 comprises various softwareprograms including but not limited to compilers, database systems, wordprocessors, business programs, videogames, and so forth. Applicationprograms provide the means by which computer resources are utilized tosolve problems, provide solutions, and process data for various users(e.g., machines, other computer systems, and/or end-users).

The operating system component 304 comprises the operating system itselfand its shell and kernel. An operating system (OS) is a special programthat acts as an intermediary between application programs and computerhardware, and the purpose of an operating system is to provide anenvironment in which a user can execute application programs. The goalof any operating system is to make the computer system convenient touse, as well as utilize the computer hardware in an efficient manner.

The operating system is generally loaded into a computer system atstartup and thereafter manages all of the application programs (orsimply “applications”) in the computer system. The application programsinteract with the operating system by requesting services via anapplication program interface (API). Some application programs enableend-users to interact with the operating system via a user interfacesuch as a command language or a graphical user interface (GUI).

An operating system traditionally performs a variety of services forapplications. In a multitasking operating system where multiple programsmay be running at the same time, the operating system determines whichapplications should run in what order and how much time should beallowed for each application before switching to another application fora turn. The operating system also manages the sharing of internal memoryamong multiple applications, and handles input and output to and fromattached hardware devices. The operating system also sends messages toeach application (and, in certain cases, to the end-user) regarding thestatus of operations and any errors that may have occurred.

An operating system's shell is the interactive end-user interface to anoperating system. A shell is the outer layer of an operating system thatis directly accessible by application programs and even directly byend-users. In contrast to a shell, the kernel is an operating system'sinnermost layer that interacts directly with the hardware components.

As well understood by those of skill in the relevant art, “files” areentities of information (including but not limited to the operatingsystem itself, as well as application programs, data sets, and so forth)that are capable of being manipulated as discrete (storable andretrievable) entities by an operating system. In modern operatingsystems, files are the basic units of storable information (e.g., data,programs, and so forth) that are manipulated by the operating system,and groups of files are organized in “folders”.

A storage platform for organizing, searching, and sharing data that canbe used with the present invention is designed to be the store for alltypes of data. Referring to FIG. 4, a storage platform 400 in accordancewith the present invention comprises a data store 402 implemented on adatabase engine 414. In one embodiment, the database engine comprises arelational database engine with object relational extensions. In oneembodiment, the relational database engine 414 comprises the MicrosoftSQL Server relational database engine.

The data store 402 implements a data model 404 that supports theorganization, searching, sharing, synchronization, and security of data.Specific types of data are described in schemas, such as schemas 440,442 and the storage platform 400 provides tools 446 for deploying thoseschemas as well as for extending those schemas.

A change tracking mechanism 406 implemented within the data store 402provides the ability to track changes to the data store. The data store402 also provides security capabilities 408 and a promotion/demotioncapability 410. The data store 402 also provides a set of applicationprogramming interfaces 412 to expose the capabilities of the data store402 to other storage platform components and application programs (e.g.,application programs 450 a, 450 b, and 450 c) that utilize the storageplatform.

The storage platform of the present invention still further comprises anapplication programming interface (API) 420, which enables applicationprograms, such as application programs 450 a, 450 b, and 450 c, toaccess all of the foregoing capabilities of the storage platform and toaccess the data described in the schemas. The storage platform API 422may be used by application programs in combination with other APIs, suchas the OLE DB API 424 and the Microsoft Windows Win32 API 426.

The storage platform 400 of the present invention may provide a varietyof services 428 to application programs, including a synchronizationservice 430 that facilitates the sharing of data among users or systems.For example, the synchronization service 430 may enable interoperabilitywith other data stores 438 having the same format as data store 402, aswell as access to data stores having other formats. The storage platform400 also provides file system capabilities that allow interoperabilityof the data store 402 with existing file systems, such as the WindowsNTFS file system 418. A SQL store 416 may also be provided.

In at least some embodiments, the storage platform 400 may also provideapplication programs with additional capabilities for enabling data tobe acted upon and for enabling interaction with other systems. Thesecapabilities may be embodied in the form of additional services 428,such as an Info Agent service 434 and a notification service 432, aswell as in the form of other utilities 436.

In at least some embodiments, the storage platform is embodied in, orforms an integral part of, the hardware/software interface system of acomputer system. For example, and without limitation, the storageplatform of the present invention may be embodied in, or form anintegral part of, an operating system, a virtual machine manager (VMM),a Common Language Runtime (CLR) or its functional equivalent, or a JavaVirtual Machine (JVM) or its functional equivalent, or other suchsoftware components in the place of or in addition to the operatingsystem in a computer system. The purpose of a hardware/softwareinterface system is to provide an environment in which a user canexecute application programs.

Through its common storage foundation, and schematized data, the storageplatform of the present invention enables more efficient applicationdevelopment for consumers, knowledge workers, and enterprises. It offersa rich and extensible programming surface area that not only makesavailable the capabilities inherent in its data model, but also embracesand extends the existing file system and database access methods.

In the description herein, and in various ones of the figures, thestorage platform 400 of the present invention may be referred to as“WinFS.” However, use of this name to refer to the storage platform issolely for convenience of description and is not intended to be limitingin any way.

The data store 402 of the storage platform 400 of the present inventionimplements a data model that supports the organization, searching,sharing, synchronization, and security of data that resides in thestore. The data model provides a mechanism for declaring objects andobject extensions and for establishing relationships between objects andfor organizing and categorizing objects.

The relational database engine 414, which in one embodiment comprisesthe Microsoft SQL Server engine, supports built-in scalar types.Built-in scalar types are “native” and “simple”. They are native in thesense that the user cannot define their own types and they are simple inthat they cannot encapsulate a complex structure. User-defined types(“UDTs”) provide a mechanism for type extensibility above and beyond thenative scalar type system by enabling users to extend the type system bydefining complex, structured types. Once defined by a user, a UDT can beused anywhere in the type system that a built-in scalar type might beused.

The storage platform schemas are mapped to UDT classes in the databaseengine store. Data store objects are mapped to UDT classes deriving fromthe Base.Item type. Extensions are also mapped to UDT classes and makeuse of inheritance. The root Extension type is Base.Extension, fromwhich all Extension types are derived.

A UDT is a CLR class—it has state (i.e., data fields) and behavior(i.e., routines). UDTs are defined using any of the managedlanguages—C#, VB.NET, etc. UDT methods and operators can be invoked inT-SQL against an instance of that type. A UDT can be the type of acolumn in a row, the type of a parameter of a routine in T-SQL, or thetype of a variable in T-SQL, for example.

Exemplary Embodiments

The present invention is directed to optimizing granular updates to UDTsor other data structures in the presence of indexes or indexed viewsover scalar expressions that represent their subfields. Exemplaryoptimization comprises detecting if such indexes or indexed views overUDT subfields are affected by a data modification statement (e.g., anUpdate command), and changes are propagated only to the dependent datastructures whose content is being modified. Aspects of the inventioninvolve detecting the presence of scalar expressions representing asubfield or subfields of a UDT inside an index or indexed viewdefinition or any other kind of dependent object, matching suchexpressions against the representation of the granular update being run,and determining whether the data structure needs to be maintained ornot.

Pursuant to receipt of a data modification command or statement, such asan Update command, it is determined which fields in a data structure,such as a UDT, computed column, indexed view, or index, for example,will be changed as a result of the command. An Update command is used inthe description of the exemplary embodiments described herein, but it iscontemplated that other types of data modification commands orstatements may be used. Only those fields that are affected by thecommand will be changed. Thus, changes are propagated to only to thosedependent data structures whose content is being modified. Treerepresentations are used to facilitate the determination as to whether asubfield in a hierarchy is changed.

For example, a computed column has a representation in tree form. Thistree representation is obtained, along with a tree representation of thedata modification statement or command. The two tree representations arecompared to determine if the computed column should be updated.

Similarly, indexed views (stored pre-computed query results) can be usedin accordance with the present invention. FIG. 5 shows a flow diagram ofan exemplary method of processing a data modification statement inaccordance with the present invention. At step 500, a data modificationstatement is received, along with a textual representation of theindexed view (which may be received or determined separately). Anoperator tree (i.e., a relational tree) that defines the indexed view(or whatever field of the data structure is being considered) is thendetermined, at step 510. At step 520, a tree representation for the datamodification statement is determined. For each of the relationaloperators in the tree determined at step 510, its child scalarexpressions are obtained, and compared to the update tree from step 520to determine if the fields of the data structure being considered arebeing updated, at step 530. If so, the query plan is modifiedaccordingly, at step 540. If the fields of the data structure beingconsidered are not to be changed or otherwise modified by the datamodification command or statement, then no change is made to the relatedportion of the query plan, at step 550.

In particular, a query processor walks the tree representation of theupdate, trying to match subfields used in the index expressions withthose being updated. If a match is found, the corresponding indexexpression is considered to be changed and a query execution plan willbe generated that propagates the update to the index. Because only thesubfields that are being changed or updated by the data modificationcommand are considered, the runtime (disk access) is thus moreefficient. Examples are provided with respect to FIGS. 8-10.

Database management systems (DBMS) allow modification of existing tablerows through Update statements or other data modification commands. Forexample, a user may specify, via a command statement, the table toupdate (called the target table), the columns to modify and theirrespective new value(s), and qualify the rows to be updated through aWhere clause, which is similar to that in a Select statement.

An example of an Update statement is “Update T set A=5 Where B=10”,which means that for each row in table T where column B is 10, column Ahas to be set to 5. A flow diagram of exemplary steps involved whenreceiving this statement in this example is shown in FIG. 6. At step600, the table T is read. A filter is applied to the table at step 610to determine which column B's are “10”. At step 620, the column A'scorresponding to the “10” column B's are “updated” to be set equal to“5”.

The updating during step 620 uses an update query plan compiled by aquery optimizer, described further below. The update query plan compiledby the query optimizer guarantees the preservation of data integrityafter its execution. This is achieved by a proper instrumentation of theplan, with actions falling in two categories, either validating aconstraint or maintaining a dependent data structure so that its contentremains consistent with the target table. DBMSs allow redundantduplication of data in secondary data structures, to allow fasterprocessing of certain categories of queries. Maintaining consistencybetween the base table and its dependent secondary structures allowssubsequent data retrieval queries to receive the same results,independently from the data source that is chosen to implement theoperation.

An Update statement can hence be seen as firing actions that were notexplicitly described in the original syntax, but that are implicitlyused in order to preserve data integrity. These actions are performed atstep 630. Typical implicit actions are: secondary index maintenance;indexed view maintenance; check and referential integrity constraintvalidation; cascading actions; full text notifications; and querynotifications. In SQL Server, the implicit update actions are desirablyperformed after modifying the base table and in appropriate order.

In order to identify the desired implicit actions to include in theupdate query plan, the SQL Server query optimizer enumerates the variouskinds of objects that have a dependency on the table being updated. Foreach object, it is determined if the object is affected by thestatement. If this is the case, the plan is modified to includeappropriate operators guaranteeing that its execution does not causedata corruption for the object. An object is considered to be affectedby the statement only if referencing one or more of the columns beingupdated.

Avoiding maintenance of objects that do not reference columns or otherdata structure fields being updated is a compile-time optimization thatavoids processing operations guaranteed to be unnecessary by the syntaxof the statement. Such optimization is desirable in order to guaranteeacceptable performance. The number of dependent objects can be veryhigh, and some of the implicit actions used to maintain them can beextremely expensive.

Query processing in accordance with the present invention is implementedin the SQL Server DBMS through the interaction of the followingexemplary components, as shown in FIG. 7: a front end (parser andalgebrizer) 700, a query optimizer 710, a query execution component 720,and a storage engine 730. The front end 700 and query optimizer 710perform at compile time, and the query execution component 720 andstorage engine 730 perform during execution time.

A SQL command or data modification statement, such as Update, isreceived (e.g., as text from a user). The front end 700 receives thequery statement in textual format, and converts it to a tree basedrepresentation describing at the logical level what the command is meantto do. In other words, a parser/algebrizer transforms the textualrepresentation of the user's commands into tree nodes. The parserprovides an update operator that internally represents the updatestatements functionally (semantically). The algebrizer provides TARGETTABLE, COLUMNS, NEW VALUES, TYPE OF UPDATE, for example.

The query optimizer 710 explores the possible alternative ways ofimplementing the query statement, trying to choose the most efficient.The query optimizer 710 produces another tree that represents, at thephysical level, the plan to implement the query statement. A queryoptimizer generates the physical plan, by transforming the tree andfinding the structural (physical) implementation that should beperformed to implement the functionality. The optimizer determines thesecondary indexes that are to be maintained (i.e., partial update indexmaintenance). The optimizer enumerates the dependencies on the TABLE(dependencies include indexed views and secondary indexes, for example).For each dependent object, the type of dependency is determined, alongwith what it is based on (e.g., based on a column(s) or anexpression(s), based on a subfield of a UDT.) If the dependency is basedon a UDT expression that extracts a subfield, then the syntax of thecommand is taken and it is determined if the dependent object is beingaffected by the update or not.

The query plan comprises appropriately interconnected query executionoperators, which function by processing the rows they receive from theirinputs, and passing the output of such processing to the operator thatfollows. The combination of the first two phases is called compilation,and its output—the query plan—is saved to memory to be reused in casethe same query is issued again in the future. Techniques likesubstituting constants with logical parameters in the query statementallow more frequent reuse of the same plan.

The query execution component 720 executes the query plan generated bythe query optimizer 710, interacting with the storage engine 730, whichis the component that ultimately stores and retrieves the data. Thequery execution component receives subtrees for index maintenance fromthe optimizer, and executes the physical tree built by the queryoptimizer. Execution is performed by a storage engine to modify the datathat is stored in the indexes, so that individual fields are updated.

Query execution plans for implementing Update statements in SQL Serverare split in two phases. The first part is read only, and responsiblefor determining which rows are to be updated. The second part consumesthe rows provided by the first, and performs the actual datamodification.

It is contemplated that the invention can be used with any type ofdependency and can be extended, among the others, to constraints overcolumns. Constraints may be user defined during table generation (e.g.,zip code is 5 digits).

When processing a regular Update statement, the SQL Server queryprocessor enumerates all the access paths dependent on the target table,extracts the list of columns used inside these dependent access paths,and verifies whether any of them is present in the assignment list. Ifthis is the case, the content of the access path is considered to bechanging, and the query execution plan is modified accordingly. Forexample, given a table T with columns (C1, C2, C3), and an index overC1+C2, an update of the form “Update T Set C1=2”, will result in a queryexecution plan that propagates the changes to the index. The plan forthe statement “Update T Set C3=10” will instead leave the indexuntouched, resulting in better performance at runtime.

This functionality is improved when the table contains one or more UDTtype columns, with index expressions using subfields of these columns,for example. A granular update is represented in the SQL Serverrelational algebra as a tree of special-purpose scalar operators(referred to herein as “UdtMultiUpdate” and “UdtPropertySetter”). Theseoperators are desirably behind the scenes and are not known to the user(the user is aware of “Update”, but not these operators). TheUdtPropertySetter scalar operator is used to set the value of a certainsubfield of a UDT column or subfield. The UdtMultiUpdate scalar operatoris used to group together updates to subfields of a UDT column orsubfield. It can be stacked in a recursive fashion to describe updatesto subfields at different levels in the UDT column.

Accessing UDT subfields in index expressions is represented by a scalaralgebra tree using the scalar operator “UdtFunction”, for example. Thisoperator can also be stacked on top of itself to drill down into a UDTstructure. The lowest node in such a tree is desirably an identifiernode representing the UDT column and each UdtFunction node on top ofthat identifier node drills down one level in the UDT structure.

The examples below illustrate the use of both theUdtMultiUpdate/UdtPropertySetter and the UdtFunction operators. As notedabove, when processing such a granular update statement, the SQL Serverquery processor will walk the tree representation of the update, tryingto match subfields used in the index expressions with those beingupdated. If it finds a match, the corresponding index expression isconsidered to be changed and a query execution plan will be generatedthat propagates the update to the index.

An example is directed to a multi-level UDT structure, and a table T hasa column “Person” of UDT type “Person”. The Person type is defined ashaving the following structure:

type Person { int ssn; // Social security number. int age; // Age.string name; // Name. type Address address; // Address. This is also aUDT type. } type Address { string street; // Street. string zip; // Zipcode. }The table also has two indexes over person.ssn and person.address.zip,respectively. Consider the following update, in which it is desired tochange some subfields in the person UDT column. A user query is:

UPDATE T SET person.(age = 10, address.( street = ‘1 Microsoft Way’, zip= 98007))Person is a UDT with subfield age, and Address is a UDT with subfieldsStreet, Zip. The above syntax (i.e., the Update command statement) getstranslated into a tree representation to perform the processing of theupdate. An exemplary scalar tree for the Update command is shown inFIGS. 8 and 9 as scalar tree 800. A scalar tree for each of the indexexpressions is also generated. The scalar tree for the first index isshown in FIG. 8 as tree 810, and the scalar tree for the second index isshown in FIG. 9 as tree 910.

As can be seen from the trees in the example, the update tree 800 drillsdown into a UDT hierarchy, while the index expression trees 810, 910represent subfields of the UDT, with scalar nodes towards the bottom ofthe tree representing subfields higher in the hierarchy. This is why theexemplary tree matching technique walks the update tree in a top-downfashion but matches subfields in the index expressions from bottom totop. An exemplary technique starts from the top of the update tree 800identifying that the UDT column being partially updated is ‘person’. Forall the index expression trees 810, 910, it then looks at the bottomnode, and tries to match this with Person. In this case, both indexexpressions match. Although the matching sequence of both indexexpression trees 810, 910 are described together, it is noted that eachindex expression is considered separately to reduce the complexity ofthe method.

Because up to this point both index expressions might be changed by theUpdate, the higher tree nodes in the index expression (and lower treenodes in the update tree 800) are now processed. Thus, both the Addressand SSN fields are matched against the child nodes of the top-levelUdtMultiUpdate. In this example, only Address matches, and it cantherefore be deduced that the first index (person.ssn) is not affectedby this Update. The process continues for the second index, and Zip isthen matched against the children of the UdtMultiUpdate(address) node.One of these children is UdtPropertySetter(zip), meaning the secondindex should be maintained.

Another example is directed to inheritance in a UDT. This example usesthe table T set forth above, with an Address UDT and subtype of Address,USAddress, as shown:

type Address { string street; // Street. string city; // City. } //USAddress is a subclass of Address; it contains all the members ofAddress. type USAddress : Address { int zip; // Zip code. }

Just like in any object-oriented programming language, the SQL ServerUDT architecture allows for subtypes of a UDT type to be stored,updated, and queried over instead of that UDT ‘base’ type. This meansthat some of the Person instances may contain a USAddress instead of thebase Address. The ‘Treat’ keyword is used to treat certain types as oneof their subtypes.

Assume a user's query is:

UPDATE T SET person.(age = 10, TREAT(address as USAddress).(street = ‘1Microsoft Way’, zip = 98007))

An exemplary update scalar tree 950 is shown in FIG. 10. The matchingtechnique described above is used in processing this example as well.Note that every field in a UDT desirably has an ordinal that is uniquefor the whole inheritance hierarchy of that UDT. In the example above,if street and city in Address have ordinal 1 and 2 respectively, thenzip will have ordinal 3, thus avoiding conflicts between derivedclasses. Subfields in the update expression are matched up withsubfields in the index expressions using their ordinal numbers. Thistechnique also allows the use of Treat in the index expressions.

CONCLUSION

The various systems, methods, and techniques described herein may beimplemented with hardware or software or, where appropriate, with acombination of both. Thus, the methods and apparatus of the presentinvention, or certain aspects or portions thereof, may take the form ofprogram code (i.e., instructions) embodied in tangible media, such asfloppy diskettes, CD-ROMs, hard drives, or any other machine-readablestorage medium, wherein, when the program code is loaded into andexecuted by a machine, such as a computer, the machine becomes anapparatus for practicing the invention. In the case of program codeexecution on programmable computers, the computer will generally includea processor, a storage medium readable by the processor (includingvolatile and non-volatile memory and/or storage elements), at least oneinput device, and at least one output device. One or more programs arepreferably implemented in a high level procedural or object orientedprogramming language to communicate with a computer system. However, theprogram(s) can be implemented in assembly or machine language, ifdesired. In any case, the language may be a compiled or interpretedlanguage, and combined with hardware implementations.

The methods and apparatus of the present invention may also be embodiedin the form of program code that is transmitted over some transmissionmedium, such as over electrical wiring or cabling, through fiber optics,or via any other form of transmission, wherein, when the program code isreceived and loaded into and executed by a machine, such as an EPROM, agate array, a programmable logic device (PLD), a client computer, avideo recorder or the like, the machine becomes an apparatus forpracticing the invention. When implemented on a general-purposeprocessor, the program code combines with the processor to provide aunique apparatus that operates to perform the functionality of thepresent invention.

While the present invention has been described in connection with thepreferred embodiments of the various figures, it is to be understoodthat other similar embodiments may be used or modifications andadditions may be made to the described embodiments for performing thesame functions of the present invention without deviating therefrom.Therefore, the present invention should not be limited to any singleembodiment, but rather construed in breadth and scope in accordance withthe appended claims.

1. A storage medium having stored thereon a data structure, comprising:a first data field containing a first tree representation of a datamodification statement to modify data within the data structure, whereinthe data modification statement comprises an update command; a seconddata field containing a second tree representation of data subfields,wherein the data subfields are subfields of an indexed view, wherein theindexed view expresses a result of a predefined query applied to thedatabase, wherein the indexed view is configured to be employed when aquery similar to the predefined query is applied to the database,wherein the second tree representation comprises a relational tree thatdefines the indexed view and the second tree representation includes aplurality of relational operators, wherein each relational operator haschild scalar expressions, wherein the second data field comprises auser-defined type (UDT) derived by way of inheritance from another UDT,the derived UDT includes each field of the another UDT and at least oneadditional field, the second tree representation references the anotherUDT and includes an operator directing that the another UDT be treatedas the derived UDT so as to access the at least one additional field; athird data field identifying the fields of the a data structure that areaffected by the data modification statement, wherein the third datafield contains the results of a comparison between the child scalarexpressions of the second data field and the first tree representationof the first data field to determine that a corresponding field of theindexed view of the second data field is affected by the datamodification statement, wherein the comparison between the child scalarexpressions of the second data field and the first tree representationof the first data field comprises walking the first tree representation,matching the subfields used in the child scalar expressions withsubfields updated in the first tree representation, and generating foreach match a query execution plan to propagate the corresponding updatedsubfield to the index; and a fourth data field containing a query planmodified in response to the third data field, wherein the query plancorresponds to the data modification statement so as to update theindexed view of the second data field to be consistent with the modifieddata within the data structure.
 2. The storage medium if claim 1,wherein walking the first tree representation of the data modificationstatement proceeds in a top-down manner and matches the fields of thedata structure from bottom to top.
 3. The storage medium of claim 2,wherein walking the first tree representation for the data modificationstatement proceeds in a top-down manner and matches the fields of thedata structure from bottom to top.
 4. A storage medium having storedthereon computer executable instructions that, when executed, perform amethod, the method comprising: receiving a data modification statementto modify data within the database; determining a tree representationfor the received data modification statement; receiving a data structurecomprising an indexed view of the database, the indexed view expressinga result of a predefined query applied to the database, the indexed viewto be employed when a query similar to the predefined query is appliedto the database; determining an operator tree as a relational tree thatdefines the received indexed view, the operator tree including aplurality of relational operators, each relational operator having childscalar expressions; and for each relational operator in the determinedoperator tree: obtaining the child scalar expressions of the relationaloperator; comparing the obtained child scalar expressions to thedetermined tree representation for the received data modificationstatement to determine that a corresponding field of the indexed view ofthe database is affected by the data modification statement, and basedthereon modifying a query plan corresponding to the data modificationstatement so as to update the indexed view of the database to beconsistent with the modified data within the database, wherein the datamodification statement comprises an update command and the datastructure comprises a user-defined type (UDT) derived by way ofinheritance from another UDT, the derived UDT including each field ofthe another UDT and at least one additional field, the operator treedetermined from the data structure referencing the another UDT andincluding an operator directing that the another UDT be treated as thederived UDT so as to access the at least one additional field, andwherein comparing the obtained child scalar expressions to thedetermined tree representation comprises walking the determined treerepresentation, matching subfields used in the child scalar expressionswith subfields updated in the determined tree representation, andgenerating for each match a query execution plan to propagate thecorresponding updated subfield to the index.
 5. The storage medium ofclaim 4, comprising determining that a field is affected by the datamodification statement by comparing the tree representation for thefields of the data structure and the tree representation for the datamodification statement.
 6. The storage medium of claim 5, whereincomparing the tree representation for the fields of the data structureand the tree representation for the data modification statementcomprises, for each relational operator in the tree representation forthe fields of the data structure, determining scalar expressions andcomparing the scalar expressions to the tree representation for the datamodification statement.
 7. The storage medium of claim 4, comprisingdetermining that a field is affected by the data modification statementby walking the tree representation for the data modification statementattempting to match fields of the data structure.
 8. The storage mediumof claim 7, wherein walking the tree representation for the datamodification statement proceeds in a top-down manner and matches thefields of the data structure from bottom to top.
 9. The storage mediumof claim 4, comprising determining that a field is affected by the datamodification statement by matching fields of the data structure withfields in the data modification statement.
 10. A method comprising:receiving a first data field containing a first tree representation of adata modification statement to modify data within the data structure,wherein the data modification statement comprises an update command;receiving a second data field containing a second tree representation ofdata subfields, wherein the data subfields are subfields of an indexedview, wherein the indexed view expresses a result of a predefined queryapplied to the database, wherein the indexed view is configured to beemployed when a query similar to the predefined query is applied to thedatabase, wherein the second tree representation comprises a relationaltree that defines the indexed view and the second tree representationincludes a plurality of relational operators, wherein each relationaloperator has child scalar expressions, wherein the second data fieldcomprises a user-defined type (UDT) derived by way of inheritance fromanother UDT, the derived UDT includes each field of the another UDT andat least one additional field, the second tree representation referencesthe another UDT and includes an operator directing that the another UDTbe treated as the derived UDT so as to access the at least oneadditional field; identifying a third data field, via a processor,wherein the third data field comprises the fields of the a datastructure that are affected by the data modification statement, whereinthe third data field contains the results of a comparison between thechild scalar expressions of the second data field and the first treerepresentation of the first data field to determine that a correspondingfield of the indexed view of the second data field is affected by thedata modification statement, wherein the comparison between the childscalar expressions of the second data field and the first treerepresentation of the first data field comprises walking the first treerepresentation, matching the subfields used in the child scalarexpressions with subfields updated in the first tree representation, andgenerating for each match a query execution plan to propagate thecorresponding updated subfield to the index; and identifying a fourthdata field, wherein the fourth data field contains a query plan modifiedin response to the third data field, wherein the query plan correspondsto the data modification statement so as to update the indexed view ofthe second data field to be consistent with the modified data within thedata structure.
 11. The storage medium if claim 10, wherein walking thefirst tree representation of the data modification statement proceeds ina top-down manner and matches the fields of the data structure frombottom to top.
 12. The storage medium of claim 11, wherein walking thefirst tree representation for the data modification statement proceedsin a top-down manner and matches the fields of the data structure frombottom to top.